package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;

/**
 * Created by ASUS on 2017/7/26.
 */

public class PrizeAttributeHorizontalAdapter extends RecyclerView.Adapter<PrizeAttributeHorizontalAdapter.ViewHolder> implements View.OnClickListener{
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private String[] datas;
    private String currents;

    public void setChoose(String c){
        currents = c;
    }

    public PrizeAttributeHorizontalAdapter(final String[] datas, final Context context, String c) {
        this.datas = datas;
        this.context = context;
        currents = c;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_lhc_prize_item1, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.number.setText(datas[position]);
        if (datas[position].equals(currents)) {
            viewHolder.number.setBackgroundResource(R.drawable.bg_corners_100_6);
            viewHolder.number.setTextColor(context.getResources().getColor(R.color.purples));
        } else {
            viewHolder.number.setBackgroundResource(0);
            viewHolder.number.setTextColor(Color.parseColor("#4a4a4a"));
        }

        viewHolder.itemView.setTag(position);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.length;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView number;

        public ViewHolder(View view){
            super(view);
            number = view.findViewById(R.id.number);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
